using MyDataSource;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Net5Wpf
{
    /// <summary>
    /// LogIn.xaml 的交互逻辑
    /// </summary>
    public partial class LogIn : Window
    {
        CloseButton Close;
        IData data;
        public LogIn()
        {
            InitializeComponent();
            data = new LocalData();
            this.MouseDown += LogIn_MouseDown;
            this.MouseUp += LogIn_MouseUp;
            SetBeiJin(AppDomain.CurrentDomain.BaseDirectory + "bitmap\\sakula.png");
        }

        private void LogIn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.MouseMove -= LogIn_MouseMove;
        }

        private void LogIn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.MouseMove += LogIn_MouseMove;
        }

        private void SetBeiJin(string path)
        {
            Image image = new Image();//新建图片对象
            image.Source = new BitmapImage(new Uri(path));//读取图片
            ImageBrush ib = new ImageBrush();//新建图片对象
            ib.ImageSource = image.Source;//赋值为背景图片
            ib.Stretch = Stretch.Fill;
            this.Background = ib;//将图片对象赋值给Canvas的Backgroun
        }
        private void LogIn_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var use = data.GetUserInfo(tb_user.Text);
            if (use == null)
            {
                use = new MeiluluSqlite.DataMode.User()
                {
                    creatTime = DateTime.Now,
                    password = "111111",
                    userid = Guid.NewGuid().ToString(),
                    username = tb_user.Text
                };
                data.AddUserInfo(use);
            }
            this.Hide();
            MainView mainView = new MainView
            {
                Use = use
            };
            mainView.Show();
        }

        private void mainView_Loaded(object sender, RoutedEventArgs e)
        {
            //Close = new CloseButton1();
            //Close.AddHandler(Button.ClickEvent, new RoutedEventHandler(CloseClick), true);
           
            ////this.AddHandler(UserControl.MouseDownEvent,new MouseButtonEventHandler(CloseClick));
            //mainCanvas.Children.Add(Close);
            //Canvas.SetLeft(Close, this.Width - Close.Width-20);
            //Canvas.SetTop(Close, Close.Height+20);
        }

        private void CloseButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }
    }
}
